/**
 *  描述：混合类
 */

export default {
  data () {
    return {
      list: [],                                           // 列表展示
      loadingText: this.$store.state.app.loadingText,         // loading 文字
      searchData: {                                       // 搜索条件
        pageNum: this.$store.state.app.page,             // 当前页码
        pageSize: this.$store.state.app.limit            // 分页大小
      },
      pageSizeOpts: [10, 20, 50, 100],
      multipleSelection: [],                              // 多选
      totalElement: this.$store.state.app.totalElement,        // 总数量
      searchDiv: false,
      searchItemStyle: { height: '36px' }
    };
  },
  computed: {
    // 删除样式
    delClasses () {
      return {
        'hy-default-btn': !this.multipleSelection.length
      };
    },
    // 分页是否展示
    pageShow () {
      return !!this.list.length;
    },
    searchListLoading () {
        return this.$store.state.app.searchListLoading;
    },
    actionLoading () {
      return this.$store.state.app.actionLoading;
    }
  },
  created () {
    this.currentChange(1);
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    clearSelecttion () {
      this.multipleSelection = [];
    },
    currentChange (val) {
      this._hyTool.csl(true);
      this.searchData.pageNum = val;
      // 获取列表
      this._getList();
    },
    pageSizeChange (pageSize) {
      this.searchData.pageSize = pageSize;
      this.search();
    },
    // 格式化数据
    callback (res) {
      res = res.data;
      if (res) {
        this.totalElement = res.total;
        this.list = res.content ? res.content : [];
      }
      setTimeout(() => {
        this._hyTool.csl(false);
      }, 500);
    },
    search (val) {
      this.currentChange(val || 1);
    },
    addOrUp (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.modalObj.title === '新增') {
            // todo 新增方法
            this.addSubmit();
          }
          if (this.modalObj.title === '编辑') {
            // todo 编辑方法
            this.modifySubmit();
          }
        }
      });
    },
    showModal (type, action) {
      this.clearFormValid();
      switch (type) {
        case 'add':
          this.modalObj.addOrModify = true;
          this.modalObj.title = '新增';
          break;
        case 'modify':
          if (!this.multipleSelection.length || this.multipleSelection.length > 1) {
            this.$Notice.warning({title: '选择错误！'});
            return;
          }
          if (action !== 'get') {
            this.getOneData(this.multipleSelection[0]);
          } else {
            this.getById(this.multipleSelection[0]);
          }
          this.modalObj.title = '编辑';
          this.modalObj.addOrModify = true;
          break;
        case 'delete':
          if (!this.multipleSelection.length || this.multipleSelection.length > 1) {
            this.$Notice.warning({title: '选择错误！'});
            return;
          }
          this.getOneData(this.multipleSelection[0]);
          break;
      }
    },
    changeSearchDiv () {
      this.searchDiv = !this.searchDiv;
      let searchHeight = this.$refs['searchForm'].$el.clientHeight;// 获取隐藏搜索项高度
      if (this.searchDiv) {
        this.searchItemStyle = { height: searchHeight + 'px' };
      } else {
        this.searchItemStyle = { height: '36px' };
      }
    }
  }
};
